Blazor multiple cascading parameters
In this video we will discuss multiple cascading parameters in Blazor. This is continuation to our previous video Part 48. Please watch Part 48 from Blazor tutorial before proceeding.
Any of the descendant components in the component tree can access the cascading value. The descendant component, that wants to access the cascading value simply declares a property of the same type, decorated with the [CascadingParameter]
attribute.
There are 2 ways to cascade values from the parent component to a child component (in fact any child component) in the component tree.
Cascading values by type
ParentComponent.razor
This parent component is passing 2 cascading values
- Style (Value is of type string)
- EmployeeAge (Value is of type int)
ChildComponent.razor
- Since the cascaded values are of different type, this child component has to simply create a property of the same type and decorate with
[CascadingParameter]
attribute. ElementStyle
property is of typestring
so it receives theStyle
cascaded value from the parent component.- Along the same lines,
EmpAge
property in the child component receives theEmployeeAge
cascaded value from the parent component.
Multiple cascading values of the same type
What if we have multiple cascading values of the same type.
ParentComponent.razor
Both the cascading values (Style and BorderStyle) are of type string.
ChildComponent.razor
ElementStyle
property in this child component receivesBorderStyle
cascaded values from the parent component.- This is because both
Style
andBorderStyle
are of typestring
and it is theBorderStyle
cascaded value that is nearest to the child component so it wins.
ChildComponent.razor
- Both the cascading parameters in this example receives the
Style
cascading value from the parent component and the reasoning is the same. - If multiple cascaded values of the same type are passed by the parent component to the child component, by default the cascaded value that is nearest to the child component in the hierarchy wins.
Cascading values by name
- Another way to access cascading values is by Name.
- Since a unique name is provided to each of the values, these values are now cascaded to the descendant components by Name.
ParentComponent.razor
ChildComponent.razor
- The child component can now decide which cascading value it wants by using the Name.
ElementStyle
cascading parameter receives theColorStyle
cascading value andH1Border
cascading parameter receives theBorderStyle
cascading value
© 2020 Pragimtech. All Rights Reserved.